<?php
/*

Summary: PHP Variant-to-PHP Val Conversion Lib (rough draft)
Author: four.zero.one.unauthorized@gmail.com
Initial Build Date: 7/01/2010
Rev Date: 7/28/2010 13:00
PHP: 5.3.3
Extensions: 
Dependancies:
References:
1) http://code.google.com/p/corporate-overload/wiki/registry_editor
2) http://us.php.net/manual/en/class.variant.php

*/

function CSTM_variant_get_php_value($variant) {
	if (!($variant instanceof VARIANT)) {
		return !trigger_error("\$variant is not a variant object type.  Value unknown!", E_USER_ERROR);
		}
	$type = (string) CSTM_variant_get_php_type($variant);
	if (!$type) {
		return !trigger_error("Variant type not identified.  Value unknown!", E_USER_ERROR);
		}
	switch ($type) {
		case 'float':
			return (float) $variant;
		case 'int':
			return (int) $variant;
		case 'string':
			return (string) $variant;
		case 'date':
			return (float) $variant;
		case 'reference':
			return $variant;
		case 'object':
		case 'array':
			foreach ($variant as $arr) {
				$return[] = $arr;
				}
			return $return;
		case 'null':
			return null;
		case 'bool':
			return (bool) $variant;
		case 'error':
			return $variant;
		default:
			return $variant;
		}
	}
	
function CSTM_variant_get_php_type($variant) {
	if (!($variant instanceof VARIANT)) {
		return !trigger_error("\$variant is not a variant object type!");
		}
	$PHP_internal_types = array(
		'float' => array(VT_R4, VT_R8, VT_DECIMAL),
		'int' => array(VT_UI1, VT_I2, VT_I4, VT_CY, VT_I1, VT_UI2, VT_UI4, VT_INT, VT_UINT),
		'string' => array(VT_BSTR),
		'date' => array(VT_DATE),
		'reference' => array(VT_UNKNOWN, VT_DISPATCH, VT_BYREF),
		'array' => array(VT_ARRAY),
		'object' => array(8204),
		'null' => array(VT_NULL, VT_EMPTY),
		'bool' => array(VT_BOOL),
		'error' => array(VT_ERROR)
		);
	$var_type = variant_get_type($variant);
	foreach ($PHP_internal_types as $key => $type) {
		if (in_array($var_type, $type, true)) {
			return $key;
			}
		}
	return "UNKNOWN_$var_type";
	}
	
?>